﻿using Sidvall.AuthorizationManager.Net.Services.V1;
using Sidvall.AuthorizationManager.Workflow;

namespace Sidvall.AuthorizationManager.Test
{
    public static class Program
    {
        #region Public Members

        #region Main

        public static void Main(string[] args)
        {
            WorkItemContext context;
            AuthorizationScope item;
            int? resourceItemRowCount, userRowCount;

            RegisterTypes();

            context = WorkItemContext.GetWorkItemContext(Properties.Settings.Default.Arguments, args);
            Sidvall.AuthorizationManager.SystemContext.Current.AddServerConnection(ServerConnection.Deserialize(Properties.Settings.Default.ServerConnection));
            try
            {
                switch (context.ActionType)
                {
                    case ActionType.ExportSecuritySettings:
                        if (string.IsNullOrWhiteSpace(context.FilePath))
                            throw new System.ArgumentException("Filepath is missing");
                        System.Console.WriteLine("Loading AuthorizationScope: " + context.AuthorizationScopeId);
                        if (context.IncludeResourceItems)
                            resourceItemRowCount = null;
                        else
                            resourceItemRowCount = 0;
                        if (context.IncludeUserItems)
                            userRowCount = null;
                        else
                            userRowCount = 0;
                        item = Sidvall.AuthorizationManager.SystemContext.Current.DataPortal.AuthorizationScopeManager.GetItemAllIncludedAsync(null, context.AuthorizationScopeId,
                            resourceItemRowCount, userRowCount).Result;
                        System.Console.WriteLine("Exporting security settings for " + item.Name + " to " + context.FilePath);
                        ExportWorkItemProvider.ExecuteAsync(context.FilePath, item, context.IncludeResourceItems, context.IncludeUserItems, true).Wait();
                        break;
                    default:
                        break;
                }
                System.Console.WriteLine("Done.");
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(ex));
            }
            if (!context.CloseWindow)
                System.Console.ReadLine();
        }

        #endregion

        #endregion
        #region Private Members

        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                //AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                //CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                //ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(null),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                //LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                //SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                //TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.Storage.LocalStorage()),

                // Client
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                //PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.AuthorizationManager.Test.Configuration.ApplicationSettings()),
            };

            // Application specific
            Sidvall.AuthorizationManager.SystemContext.Current.DataPortalStrategyFacade.ServerConnections.Clear();
        }

        #endregion

        #endregion
    }
}
